package au.edu.adelaide.mips_soft_core.board_interface_pc.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import au.edu.adelaide.mips_soft_core.board_interface_pc.core.Core;

public class DownloadMemoryPanel extends JPanel implements ActionListener{
JComboBox<Integer> memoryModuleSelector = new JComboBox<Integer>(new Integer[]{1});
	
	JButton selectFileButton = new JButton("Select ASM File");
	JButton downloadButton = new JButton("DOWNLOAD!");
	
	File file = null;
	
	UI ui;
	
	public DownloadMemoryPanel(UI ui){
		super(new GridBagLayout());
		this.ui = ui;
		addComponent(this, new JLabel("Memory Module"), 0, 0, 1, 1);
		addComponent(this, memoryModuleSelector, 1, 0, 1, 1);
		addComponent(this, new JLabel("Available Size"), 0, 1, 1, 1);
		addComponent(this, new JLabel("500 words (2k bytes)"), 1, 1, 1, 1);
		addComponent(this, selectFileButton, 0, 2, 2, 1);
		addComponent(this, new JLabel("Free Space Available"), 0, 3, 1, 1);
		addComponent(this, new JLabel("50 words"), 1, 3, 1, 1);
		addComponent(this, new JLabel("Fill Empty Space With"), 0, 4, 1, 1);
		addComponent(this, new JTextField("0xFFFFFFFF"), 1, 4, 1, 1);
		addComponent(this, downloadButton, 0, 5, 2, 1);
		this.downloadButton.addActionListener(this);
		this.selectFileButton.addActionListener(this);
	}
	
	public void updatePanel(){
		this.memoryModuleSelector.removeAllItems();
		int n = ui.connectedDevice.numberOfMemoryModules();
		for(int i = 0; i < n; i++){
			this.memoryModuleSelector.addItem(i);
		}
	}
	
	private void addComponent(JPanel panel, JComponent component, int x, int y, int width, int height){
		 GridBagConstraints c = new GridBagConstraints();
		 c.gridx = x;
		 c.gridy = y;
		 c.gridheight = height;
		 c.gridwidth = width;
		 panel.add(component,  c);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.selectFileButton){
			JFileChooser jfc = new JFileChooser("Select File");
			int result = jfc.showSaveDialog(this);
			if(result != JFileChooser.APPROVE_OPTION){
				return;
			}
			this.file = jfc.getSelectedFile();
		}else if(e.getSource() == this.downloadButton){
			try {
				FileOutputStream outFile = new FileOutputStream(this.file);
				this.ui.connectedDevice.downloadFromBoardMemory(this.memoryModuleSelector.getSelectedIndex(), 0, this.ui.connectedDevice.sizeOfMemoryModule((byte)this.memoryModuleSelector.getSelectedIndex()) * 4, outFile);
				outFile.close();
				Core.getCore().getLogger().write("DONE Downloading From BOARD", Core.LOG_LEVEL_MESSAGE);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
	}

}
